package cn.demomaster.quickpermission.util

import android.app.Activity
import android.content.Context

object PreferenceManager {
    private const val PREF_NAME = "Quick_Permission_preferences"
    private const val KEY_PERMISSION_REQUESTED = "permission_requested"

    fun hasRequestedPermission(context: Context,permission: String): Boolean {
        val prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
        return prefs.getBoolean("permission_$permission", false)
    }

    fun setPermissionRequested(context: Context,permission: String, value: Boolean) {
        val prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
        prefs.edit().putBoolean("permission_$permission", value).apply()
    }
    fun getDeniedCount(context: Context,p: String): Int {
        val prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
        var count = prefs.getInt("permission_denied_count$p", 0)
        return count
    }
    fun onPermissionDenied(context: Context,p: String) {
        val prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
        var count = prefs.getInt("permission_denied_count$p", 0)
        count++
        prefs.edit().putInt("permission_denied_count$p", count).apply()
    }
    fun isBlackPermission(context: Context,p: String): Boolean{
        val prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
        var count = prefs.getInt("permission_denied_count$p", 0)
        return count >= 2
    }

    fun onPermissionGranted(activity: Activity, p: String) {
        val prefs = activity.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
        prefs.edit().putInt("permission_denied_count$p", 0).apply()
    }
}